package com.ssm.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ssm.entity.Organization;
import com.ssm.service.OrganizationService;
import com.ssm.util.JSONResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/organization")
public class OrganizationController {

    @Resource
    private OrganizationService organizationService;

    @RequestMapping("/add")
    public JSONResult addOrganization(Organization organization){
        organizationService.addOrganization(organization);
        return JSONResult.success();
    }

    @RequestMapping("/update")
    public JSONResult updateOrganization(Organization organization){
        organizationService.updateOrganization(organization);
        return JSONResult.success();
    }

    /**
     * 数据单查
     */
    @RequestMapping("/get/{id}")
    public JSONResult getOrganization(@PathVariable("id") Integer id){
        return JSONResult.success(organizationService.getOrganization(id));
    }

    @RequestMapping("/removeBatch")
    public JSONResult removeBatchOrganization(@RequestParam("ids[]") Integer[] ids){
        organizationService.removeBatchOrganization(ids);
        return JSONResult.success();
    }

    @RequestMapping("/list")
    public JSONResult listOrganization(@RequestParam(value = "pageNumber",required = false,defaultValue = "1") Integer pageNum,
                                       @RequestParam(value = "pageSize",required = false,defaultValue = "10") Integer pageSize,
                                       Organization organization){
        //使用分页助手
        PageHelper.startPage(pageNum, pageSize);
        //查询当前页面的记录信息
        List<Organization> list =  organizationService.listOrganization(organization);
        //初始化分页信息对象，并将当前页面的记录信息存入该对象
        PageInfo<Organization> info = new PageInfo<>(list);
        return JSONResult.success(list, info.getTotal());

    }
    @RequestMapping("/username")
    public JSONResult name(String name){
        int exists = organizationService.selectOrganizationByname(name);
        if(exists==0){
            return JSONResult.success();
        }
        return JSONResult.error();
    }
}
